home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 5_5.lha / 5_5 / 5_5c1.c < prev    next >
Text File  |  1993-08-08  |  904b  |  61 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / print out the tree in infix format
  6. tatic void infixprint(tree* head)
  7.  
  8.    cout << "( ";
  9.    if (head)
  10. switch (head->type)
  11.     {
  12.     case PLUS:
  13.     case DIV:
  14.     case MUL:
  15.     infixprint (head->left);
  16.     cout << chr (head->type) << " ";
  17.     infixprint (head->right);
  18.     break;
  19.  
  20.     case MINUS:
  21.     if (head->right)
  22.         {
  23.         infixprint (head->left);
  24.         cout << "- ";
  25.         infixprint (head->right);
  26.         }
  27.  
  28.     else
  29.         {
  30.         cout << "- ";
  31.         infixprint (head->left);
  32.         }
  33.     break;
  34.  
  35.     case NUMBER:
  36.     cout << head->value;
  37.     break;
  38.  
  39.     case LP:
  40.     infixprint (head->left);
  41.     break;
  42.  
  43.     case RP:
  44.     case END:
  45.     default:
  46.     error ("invalid type within tree");
  47.     break;
  48.     }
  49.  
  50.    else
  51. error ("NULL node found");
  52.  
  53.    cout << " ) ";
  54.  
  55.  
  56. oid expr:: print()
  57.  
  58.    infixprint(head);
  59.  
  60.  
  61.